package cn.mastor.controller;

import cn.mastor.domain.User;
import cn.mastor.redisUtilService.RedisUtilsService;
import cn.mastor.service.IUserService;
import cn.mastor.vo.UserQuerys;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import mihoyo.result.JSONResult;
import mihoyo.result.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;
    @Autowired
    RedisUtilsService redisUtilsService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(userService.selectById(id));
    }
    @RequestMapping(value = "/{gameId}/{sex}",method = RequestMethod.GET)
    public JSONResult getByGame(@PathVariable("gameId")Long id,@PathVariable("sex")Integer sex){
        return JSONResult.success(userService.selectByGameId(id,sex));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(userService.selectList(null));
    }
    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/onlineList/{start}/{stop}",method = RequestMethod.GET)
    public JSONResult onlineList(@PathVariable("start")Integer start,@PathVariable("stop")Integer stop){
        return JSONResult.success(redisUtilsService.getListPage(RedisUtilsService.ENT_INFO_LIST,start,stop));
    }
    @RequestMapping(value = "/addOnlineList/{id}",method = RequestMethod.GET)
    public JSONResult onlineList(@PathVariable("id")Integer start){
        Date date = new Date();
        return JSONResult.success(redisUtilsService.addZSet(RedisUtilsService.ENT_INFO_LIST,start.toString(),(double) date.getTime()));
    }
    @RequestMapping(value = "/delOnlineList/{id}",method = RequestMethod.GET)
    public JSONResult delOnlineList(@PathVariable("id")Integer start){
        Date date = new Date();
        return JSONResult.success(redisUtilsService.delZSet(RedisUtilsService.ENT_INFO_LIST,start.toString()));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserQuerys query){
        Wrapper<User> wrapper= new EntityWrapper();
        if (query.getGameId()!=null){
            wrapper.eq("game_id",query.getGameId());
        }
        if (query.getSex()!=null){
            wrapper.eq("sex",query.getSex());
        }
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }
}
